//
//  PayMethod.m
//  YHGJ
//
//  Created by glavesoft on 17/3/21.
//  Copyright © 2017年 LHX. All rights reserved.
//

#import "PayMethod.h"
#import "WXApi.h"
#import <AlipaySDK/AlipaySDK.h>

@implementation PayMethod

+(void)AliPayWithOrder:(NSString *)orderStr{
    
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:@"Us" callback:^(NSDictionary *resultDic) {
        NSString * resultstate =[resultDic objectForKey:@"resultStatus"];
        NSLog(@"reslut = %@",[resultDic objectForKey:@"resultStatus"]);
        if ([resultstate isEqualToString:@"9000"]){
            
            [UIWindow.keyWindow us_makeAlert:@"付款成功" subTitle:nil];

        }else{
            [UIWindow.keyWindow us_makeAlert:@"付款失败请重新支付" subTitle:nil];
 
        }
        
    }];
    
}


+(void)WXPayWithOrder:(WXPayModel *)WXPayModel{

    
    if (WXApi.isWXAppInstalled) {
        
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.openID              = WXAPPID;
        req.partnerId           = @"1627813552";
        req.prepayId            = WXPayModel.prepayid;
        req.nonceStr            = WXPayModel.noncestr;
        
        
//        NSDate *datenow = [NSDate date];
//        NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
//        UInt32 timeStamp =[timeSp intValue];
        req.timeStamp = (UInt32)WXPayModel.timestamp;
        
        req.package             = @"Sign=WXPay";
        
        req.sign =WXPayModel.sign;
        
        [WXApi sendReq:req completion:^(BOOL success) {
            
        }];
        
        
    }else{
        [UIWindow.keyWindow us_makeAlert:@"您尚未安装微信客户端" subTitle:nil];
     
    }
    
}
 

-(void)onResp:(BaseResp *)resp
{
//    NSString *strMsg = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果，实际支付结果需要去微信服务器端查询
        switch (resp.errCode) {
            case WXSuccess:
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"KpaySuccess" object:nil];
                NSLog(@"支付成功－PaySuccess，retcode = %d", resp.errCode);
            }
                break;
            default:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"KpayFail" object:nil];
                NSLog(@"错误，retcode = %d, retstr = %@", resp.errCode,resp.errStr);
                break;
        }
        
        
    }
}


@end
